package com.opengate.base.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * @author 曳戈泰尔
 * @version 1.0
 * @description 日志输出json格式化工具类
 * @date 2023/4/26 5:46 PM
 */
@Slf4j
public class LogJsonToStrUtil {
    /**
     * 通过json格式化参数，返回字符串
     *
     * @param objects 数据集合
     * @param maxTotalLen 最大总长度，超过这个长度就进行截取
     * @param maxNowLen 最大当前字段长度，单个字段值超过这个长度就对当前字段进行截取
     * @return StringBuffer
     */
    public static StringBuffer parseObjs(Object[] objects, Integer maxTotalLen, Integer maxNowLen) {
        StringBuffer stringBuffer;
        try {
            stringBuffer = parseJsonObjs(objects, maxTotalLen, maxNowLen);
        } catch (Exception e) {
            log.warn("转换参数输出异常，换其他方式，原因：{}", e.getMessage());
            String strObjs = Arrays.toString(objects);
            stringBuffer =
                    new StringBuffer(
                            strObjs.length() > maxTotalLen
                                    ? strObjs.substring(0, maxTotalLen)
                                    : strObjs);
        }
        return stringBuffer;
    }

    /**
     * 通过json格式化参数，返回字符串
     *
     * @param objects 数据集合
     * @param maxTotalLen 最大总长度，超过这个长度就进行截取
     * @param maxNowLen 最大当前字段长度，单个字段值超过这个长度就对当前字段进行截取
     * @return
     */
    public static StringBuffer parseJsonObjs(
            Object[] objects, Integer maxTotalLen, Integer maxNowLen) {
        StringBuffer stringBuffer = new StringBuffer();
        if (objects == null || objects.length == 0) {
            return stringBuffer;
        }
        String objStr =
                JSON.toJSONString(
                        objects,
                        SerializerFeature.QuoteFieldNames,
                        SerializerFeature.DisableCircularReferenceDetect,
                        SerializerFeature.WriteDateUseDateFormat);
        // 总长度没超过限制长度，不需要进行处理
        if (objStr.length() <= maxTotalLen) {
            return new StringBuffer(objStr);
        }
        String[] strs = objStr.split("\":\"");
        if (strs.length > 1) {
            stringBuffer.append(strs[0]);
            stringBuffer.append("\":\"");
            // 双引号关键字
            String syhKey = "#2002111341#";
            // 正斜杠关键字
            String zxgKey = "#2002111442#";
            // 省略
            String slKey = "#超长省略#";
            int nowMaxLenGai = maxNowLen + 20;
            for (int i = 1; i < strs.length; i++) {
                String str = strs[i];
                if (str.length() <= nowMaxLenGai) {
                    stringBuffer.append(str);
                    if (i < strs.length - 1) {
                        stringBuffer.append("\":\"");
                    }
                    // 本参数长度未达到要处理的标准
                    continue;
                }
                str = str.replaceAll("\\\\\\\\", zxgKey).replaceAll("\\\\\"", syhKey);
                String beginStr = str.substring(0, str.indexOf("\""));
                String endStr = str.substring(str.indexOf("\""));
                beginStr = beginStr.replaceAll(zxgKey, "\\\\\\\\").replaceAll(syhKey, "\\\\\"");
                if (beginStr.length() > maxNowLen) {
                    beginStr = beginStr.substring(0, maxNowLen) + slKey;
                }
                stringBuffer.append(beginStr);
                stringBuffer.append(
                        endStr.replaceAll(zxgKey, "\\\\\\\\").replaceAll(syhKey, "\\\\\""));
                if (i < strs.length - 1) {
                    stringBuffer.append("\":\"");
                }
            }
        } else {
            stringBuffer = new StringBuffer(objStr.substring(0, maxTotalLen));
        }
        return stringBuffer;
    }
}
